package cn.always.xiajia.framework.web.core.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import cn.always.xiajia.framework.common.util.ExtServletUtil;

/**
 * Request Body 缓存 Filter，实现它的可重复读取
 * 
 * @author xgj
 *
 */
public class CacheRequestBodyFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws IOException, ServletException {
		filterChain.doFilter(new CacheRequestBodyWrapper(request), response);
	}

	@Override
	protected boolean shouldNotFilter(HttpServletRequest request) {
		// 只处理 json 请求内容
		return !ExtServletUtil.isJsonRequest(request);
	}

}
